// pages/my/my.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 用户信息
    userInfo: {},
    // 用户播放记录
    recordPlayList: [],
    musiclist:[],//播放记录的音乐列表
    idslist:[],//喜欢的音乐id列表
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      // 更新userInfo的状态
      
      this.setData({
        userInfo: JSON.parse(userInfo)
      })
      // console.log(this.data.userInfo.userId)
     this.usePlayrecordList(this.data.userInfo.userId)
    }
    let userId = wx.getSystemInfoSync('userId')
    if(userId){
      this.getmylikemusiclist(userId)
    }
  },
  //用户播放记录
 async usePlayrecordList(userid){
    let recordPlayList =[];
  let res = await request('/user/record', { uid:userid, type:1 })
   console.log(res)
      console.log(res.weekData)
   
      recordPlayList = res.weekData
   let musiclist = [];
   for (let i = 0; i < recordPlayList.length; i++) {
     musiclist[i] = recordPlayList[i].song
   }
   this.setData({
     musiclist
   })
      
      //map修改数据  添加唯一标识
      if (recordPlayList.length>10){
      
         let index = 0;
        recordPlayList = recordPlayList.splice(0, 10).map(item => {
           item.id = index++;
           return item;
         })

       }else{
      
         let index = 0
        recordPlayList = recordPlayList.map(item => {
           item.id = index++;
           return item;
         })
      
       }
      console.log(recordPlayList)
      this.setData({
        recordPlayList
      })


  },
  //跳转到登录
  login(){
    wx.navigateTo({
      url: '../login/login',
    })
  },

  //获取喜欢歌曲的列表
 async getmylikemusiclist(userId){
   let { idslist} = this.data
   let res = await request('/likelist',{uid:userId})
   console.log(res)
   idslist = res.ids
   this.setData({
     idslist
   })
  },
  //点击进入我喜欢的音乐界面
  intomylike(){
  let {idslist} = this.data
      wx.navigateTo({
        url: '/pages/mylike/mylike?idslist=' + idslist,
      })

  },
  //跳转到播放音乐
  playmusic(event) {
    let { songid, index } = event.currentTarget.dataset;
    let { musiclist} = this.data
   
    wx.setStorageSync('playingMusicList', musiclist);
    console.log(songid)
    console.log(index)
    wx.navigateTo({
      url: '/pages/songdetail/songdetail?songid=' + songid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})